package com.example.ftdatabase;

import java.util.Date;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;

public class EditListActivity extends ListActivity {
	private TaskTranslator translator;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_list);
        
        translator = new TaskTranslator(this);
        translator.open();
        
        List<Task> values = translator.getAllTasks();
        
        // Use the SimpleCursorAdapter to show the
        // elements in a ListView
     	ArrayAdapter<Task> adapter = new ArrayAdapter<Task>(this,
     			android.R.layout.simple_list_item_1, values);
     	setListAdapter(adapter);
    }

    public void onClick(View view) {
		@SuppressWarnings("unchecked")
		ArrayAdapter<Task> adapter = (ArrayAdapter<Task>) getListAdapter();
		Task t;
		switch (view.getId()) {
		case R.id.add:
			t = translator.createTask("dummy task", 5, new Date(), true);
			adapter.add(t);
			break;
		case R.id.delete:
			if (getListAdapter().getCount() > 0) {
				t = (Task) getListAdapter().getItem(0);
				translator.deleteTask(t);
				adapter.remove(t);
			}
			break;
		}
		adapter.notifyDataSetChanged();
    }
    
	@Override
	protected void onResume() {
		translator.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		translator.close();
		super.onPause();
	}
}
